/*
 ============================================================================
 Name		: CustomControlExmAppView.cpp
 Author	  : William Zhang
 Copyright   : all rights reserved.
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <eikcmbut.h>
#include <aknutils.h>
#include <akniconutils.h>
#include <button_icons.mbg>
#include "CustomControlExmAppView.h"
#include "ButtonControl.h"

const TInt KButtonCount = 8;
_LIT(KButtonIconFile,"\\resource\\apps\\button_icons.mif");

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCustomControlExmAppView* CCustomControlExmAppView::NewL(const TRect& aRect)
	{
	CCustomControlExmAppView* self = CCustomControlExmAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCustomControlExmAppView* CCustomControlExmAppView::NewLC(const TRect& aRect)
	{
	CCustomControlExmAppView* self = new (ELeave) CCustomControlExmAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCustomControlExmAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();	
	
	TFileName svgFile(KButtonIconFile);
	User::LeaveIfError(CompleteWithAppPath(svgFile));
	
	for(TInt i = 0; i < KButtonCount; i++)
		{
		TRect rect(TPoint(i*73,200),TSize(72,72));
		CButtonControl* button = CButtonControl::NewL(rect,this);

		AknIconUtils::CreateIconL(iSvgBmp,iSvgBmpMask,svgFile,
		                          EMbmButton_iconsMenu + i*2, EMbmButton_iconsMenu_mask + i*2);
		AknIconUtils::SetSize(iSvgBmp,TSize(72,72),EAspectRatioNotPreserved);
		AknIconUtils::SetSize(iSvgBmpMask,TSize(72,72),EAspectRatioNotPreserved);
		button->SetButtonIcon(iSvgBmp,iSvgBmpMask);
		
		iButtonArray.Append(button);
		iSvgBmp = NULL;
		iSvgBmpMask = NULL;
		button = NULL;
		}
	
	// Set the windows size
	SetRect(aRect);
	
	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::CCustomControlExmAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCustomControlExmAppView::CCustomControlExmAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::~CCustomControlExmAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CCustomControlExmAppView::~CCustomControlExmAppView()
	{

	for(TInt i = 0; i < KButtonCount; i++)
		{
		CButtonControl* &button = iButtonArray[i];
		delete button;
		button = NULL;
		}
	iButtonArray.ResetAndDestroy();
	
//	delete iSvgBmp;
//	delete iSvgBmpMask;
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CCustomControlExmAppView::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();	
	
	// Gets the control's extent
	TRect drawRect(Rect());
		
	// Clears the screen
	gc.Clear(drawRect);	
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CCustomControlExmAppView::SizeChanged()
	{
	DrawNow();
	}

// -----------------------------------------------------------------------------
// CCustomControlExmAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CCustomControlExmAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	
	
	// Call base class HandlePointerEventL()
	CCoeControl::HandlePointerEventL(aPointerEvent);
	}

TInt CCustomControlExmAppView::CountComponentControls() const
    {
    return 5;
    }

CCoeControl* CCustomControlExmAppView::ComponentControl(TInt aIdx) const
    {
	switch(aIdx)
		{
		case 0:
			return iButtonArray[0];
			break;
		case 1:
			return iButtonArray[1];
			break;
		case 2:
			return iButtonArray[2];
			break;
		case 3:
			return iButtonArray[3];
			break;
		case 4:
			return iButtonArray[4];
			break;
		default:
			return NULL;
			break;
		}
    }
// End of File
